package com.dingreading.cloud.admin.controller;

import com.dingreading.cloud.admin.entity.AdminButton;
import com.dingreading.cloud.admin.service.AdminButtonService;
import com.dingreading.cloud.admin.service.AdminRoleButtonService;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.DescribeException;
import com.dingreading.cloud.common.util.PageBeanDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/system/adminButton")
@Api(tags = "总部-总部按钮管理")
public class AdminButtonController {

    @Resource
    private AdminButtonService buttonService;
    @Resource
    private AdminRoleButtonService roleButtonService;

    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList", produces = "application/json; charset=UTF-8")
    public R<Object> pageList(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "按钮名/描述") @RequestParam(value = "keywords", required = false) String keyword,
            @ApiParam(value = "角色id") @RequestParam(value = "roleId", required = false) Long roleId
    ) {
        Page<AdminButton> page = buttonService.pageList(pageUtil, keyword);

        List<AdminButton> list = page.getRecords();
        if (CollectionUtils.isNotEmpty(list) && roleId != null && roleId > 0) {
            List<Long> buttonIds = list.stream().map(AdminButton::getId).collect(Collectors.toList());

            List<Object> buttonIdList = roleButtonService.getByRoleIdAndButtonIds(roleId, buttonIds);

            for (AdminButton button : list) {
                button.setAddStatus(buttonIdList.contains(button.getId()) ? 1 : 0);
            }
        }
        PageBeanDto data = new PageBeanDto();
        data.setTotal(page.getTotalRow());
        data.setList(list);
        return R.ok(data);
    }

    @ApiOperation(value = "获取信息")
    @PostMapping(value = "/getBean", produces = "application/json; charset=UTF-8")
    public R<Object> getBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "角色功能id") @RequestParam(value = "id") Long id
    ) {
        AdminButton data = this.buttonService.getById(id);
        return R.ok(data);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/dropBean", produces = "application/json; charset=UTF-8")
    public R<Object> dropBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "角色功能id") @RequestParam(value = "id") Long id
    ) {
        AdminButton data = buttonService.getById(id);
        if (data == null) return R.fail("获取失败");

        int l = buttonService.dropBean(id);

        if (l <= 0)
            return R.fail("删除失败");
        roleButtonService.dropByButtonId(id);

        return R.ok();
    }

    @Transactional(rollbackFor = DescribeException.class)
    @ApiOperation(value = "添加/修改按钮")
    @PostMapping(value = "/newBean", produces = "application/json; charset=UTF-8")
    public R<Object> newBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "按钮id") @RequestParam(value = "id", required = false) Long id,
            @ApiParam(value = "按钮名称") @RequestParam(value = "name") String name,
            @ApiParam(value = "描述") @RequestParam(value = "remark", required = false) String remark
    ) {
        AdminButton data = null;
        if (id == null || id <= 0) {
            data = new AdminButton();
        } else {
            data = buttonService.getById(id);
            if (data == null) return R.fail("获取失败");
        }
        data.setName(name);
        data.setRemark(remark);
        buttonService.saveOrUpdate(data);
        return R.ok();
    }

}
